package com.iot.inclinometer.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.iot.inclinometer.R;


/**
 * “设置权限检测”
 * 
 * @author WXR
 * 
 */

public class ConfigurationLicenseUI extends Activity {
	private EditText key_edit= null;       //输入密码，才能获得设置权限
	
	private Button okBtn;          	   //确定按钮
	private Button cancelBtn;               //取消按钮
	
	private InputMethodManager inputMethodManager;       //控制键盘隐藏
	
	protected void onCreate(Bundle savedInstanceState) {
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.configuration_license_activity);
		ConfigurationLicenseUI.this.setFinishOnTouchOutside(false);  //使该对话框样式的activity在点空白处时，不消失

		inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		initView();   	 //初始化控件
		setBtnListener();//设置监听
	}
	
	/**
	 * 初始化控件
	 */
	private void initView() {
		key_edit = (EditText) findViewById(R.id.key_edit);
		okBtn = (Button) findViewById(R.id.ok_btn);
		cancelBtn = (Button) findViewById(R.id.cancel_btn);
	}
	
	/**
	 * 设置保存按钮的点击监听
	 */
	private void setBtnListener() {
		okBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
//				inputMethodManager.hideSoftInputFromWindow(ConfigurationLicenseUI.this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
				inputMethodManager.hideSoftInputFromWindow(key_edit.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
				String userKey=key_edit.getText().toString().trim();
				if(userKey.equals("jgkj123."))
				{
					Intent intent = new Intent(ConfigurationLicenseUI.this,ConfigurationUI.class);
					startActivity(intent);
					ConfigurationLicenseUI.this.finish();
				}else
				{
					Toast.makeText(ConfigurationLicenseUI.this, "密钥错误，请重新输入！", Toast.LENGTH_SHORT).show();
				}
			}
		});
		cancelBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
//				inputMethodManager.hideSoftInputFromWindow(ConfigurationLicenseUI.this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
				inputMethodManager.hideSoftInputFromWindow(key_edit.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
				ConfigurationLicenseUI.this.finish();
			}
		});		
	}
	
	
	/**
	 * 点击空白处时，隐藏键盘
	 * 
	 *
	 */
	@Override
    public boolean onTouchEvent(MotionEvent event){
        if(event.getAction()==MotionEvent.ACTION_DOWN){
            if(ConfigurationLicenseUI.this.getCurrentFocus() != null && ConfigurationLicenseUI.this.getCurrentFocus().getWindowToken() != null){
                    inputMethodManager.hideSoftInputFromWindow(ConfigurationLicenseUI.this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
        return super.onTouchEvent(event);
    }
	
}